package com.ruoyi.web.controller.server;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.enums.PositionStatusEnum;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysEnterprise;
import com.ruoyi.system.service.ISysEnterpriseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPosition;
import com.ruoyi.system.service.ISysPositionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗位Controller
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@RestController
@RequestMapping("/system/position")
public class SysPositionController extends BaseController
{
    @Autowired
    private ISysPositionService sysPositionService;

    @Autowired
    private ISysEnterpriseService enterpriseService ;

    /**
     * 查询岗位列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysPosition sysPosition)
    {
        startPage();
        List<SysPosition> list = sysPositionService.selectSysPositionList(sysPosition);
        return getDataTable(list);
    }

    /**
     * 查询自己企业岗位列表
     */
    @GetMapping("/enterpriseList")
    public TableDataInfo enterpriseList(SysPosition sysPosition)
    {
        startPage();
        ArrayList<Long> longs = null;
        Long userId = SecurityUtils.getUserId();
        if (!SecurityUtils.isAdmin(userId)) {
            LambdaQueryWrapper<SysEnterprise> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysEnterprise::getUserId ,userId) ;
            List<SysEnterprise> list = enterpriseService.list(wrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                longs = new ArrayList<>(list.stream().collect(Collectors.toMap(SysEnterprise::getId, SysEnterprise::getId)).keySet()) ;
                sysPosition.setEnterpriseIds(longs);
            }else {
                ArrayList<Long> arrayList = new ArrayList<>();
                arrayList.add(0L) ;
                sysPosition.setEnterpriseIds(arrayList);
            }
        }
        List<SysPosition> list = sysPositionService.enterpriseList(sysPosition);
        for (SysPosition position : list) {
            position.setStatus(PositionStatusEnum.getPositionStatusName(position.getStatus()));
        }
        for (SysPosition position : list) {
            position.setEnterprise(enterpriseService.getById(position.getId())) ;
        }
        return getDataTable(list);
    }

    /**
     * 导出岗位列表
     */
    @PreAuthorize("@ss.hasPermi('system:position:export')")
    @Log(title = "岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPosition sysPosition)
    {
        List<SysPosition> list = sysPositionService.selectSysPositionList(sysPosition);
        ExcelUtil<SysPosition> util = new ExcelUtil<SysPosition>(SysPosition.class);
        util.exportExcel(response, list, "岗位数据");
    }

    /**
     * 获取岗位详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysPositionService.selectSysPositionById(id));
    }


    @PostMapping("/changePositionStatus")
    public AjaxResult changePositionStatus(@RequestBody SysPosition sysPosition)
    {
        return success(sysPositionService.changePositionStatus(sysPosition));
    }
    /**
     * 新增岗位
     */
    @Log(title = "岗位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPosition sysPosition)
    {
        return toAjax(sysPositionService.insertSysPosition(sysPosition));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@ss.hasPermi('system:position:edit')")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPosition sysPosition)
    {
        return toAjax(sysPositionService.updateSysPosition(sysPosition));
    }

    /**
     * 删除岗位
     */
    @Log(title = "岗位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysPositionService.deleteSysPositionByIds(ids));
    }
}
